package com.sanjin.service.impl;

import com.sanjin.entity.WxMiniUser;
import com.sanjin.exception.MyException;
import com.sanjin.repository.WxMiniUserRepository;
import com.sanjin.service.WxMiniUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/8 13:04
 */
@Service
public class WxMiniUserServiceImpl implements WxMiniUserService {

    @Autowired
    private WxMiniUserRepository wxMiniUserRepository;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public WxMiniUser queryWxMiniUserByWxMiniUserId(Integer wxMiniUserId) {

        return wxMiniUserRepository.queryWxMiniUserById(wxMiniUserId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public WxMiniUser updateWxMiniUser(WxMiniUser wxMiniUser) {
        if (wxMiniUser.getId() == null)
            throw new MyException("wxMiniUser.id 不能为null");
        return wxMiniUserRepository.save(wxMiniUser);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public WxMiniUser createWxMiniUser(WxMiniUser wxMiniUser) {
        if (wxMiniUser.getId() != null)
            throw new MyException("wxMiniUser.id 必须为null");

        // 用户的默认车牌号默认设置为 “null”
        wxMiniUser.setCarLicenseNumber("null");
        return wxMiniUserRepository.save(wxMiniUser);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<WxMiniUser> queryWxMiniUsers() {
        return wxMiniUserRepository.findAll();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public WxMiniUser queryWxMiniUserByOpenid(String openid) {
        return wxMiniUserRepository.queryWxMiniUserByOpenid(openid);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public WxMiniUser queryWxMiniUserByCarLicenseNumber(String carLicenseNumber) {
        return wxMiniUserRepository.queryWxMiniUserByCarLicenseNumber(carLicenseNumber);
    }
}
